package offer;

import java.util.ArrayList;
import java.util.List;

public class offerII_0065 {
    public int minimumLengthEncoding(String[] words) {
        List<String> list=new ArrayList<>();
        list.add(words[0]);
        for(int i=1;i< words.length;++i){
            for(int j=0;j<list.size();++j){
                if(replaceword(list.get(j),words[i])){
                    if(words[i].length()>list.get(j).length()){
                        list.set(j,words[i]);
                        break;
                    }
                    else break;
                }
                if(j==list.size()-1){
                    list.add(words[i]);
                }
            }
        }
        int len=0;
        for(int i=0;i<list.size();++i){
            len+=list.get(i).length();
            len+=1;
        }
        return len;
    }
    public boolean replaceword(String s,String word){
        int wordlen=word.length();
        int slen=s.length();
        for(int i=0;i<Math.min(wordlen,slen);++i){
            if(s.charAt(slen-1-i)!=word.charAt(wordlen-1-i)) return false;
        }
        return true;
    }
}
